package com.lottery.ruok.lottrey.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.lib.dialog.BottomDialog;
import com.android.lib.dialog.Builder;
import com.applozic.mobicomkit.contact.AppContactService;
import com.applozic.mobicomkit.uiwidgets.conversation.ConversationUIService;
import com.applozic.mobicomkit.uiwidgets.conversation.activity.ConversationActivity;
import com.applozic.mobicomkit.uiwidgets.conversation.messageUtils.ChatRecordUtils;
import com.applozic.mobicomkit.uiwidgets.conversation.messageUtils.SearchRecordUtils;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.RecodeChatAdapter;
import com.lottery.ruok.lottrey.adapter.recyclleradapter.RecyclerItemClickListener;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.AddChatAdminResquest;
import com.lottery.ruok.lottrey.retrofitModule.request.ResumeChatUserRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomUsersResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.OperationSuccessResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import utils.Utils;
/*
* 聊天记录
* */
public class ChatRecodeActivity extends BaseActivity {

    @BindView(R.id.view_recycler)
    RecyclerView viewRecycler;
    private RecodeChatAdapter recodeChatAdapter;
    private LinearLayoutManager linearLayoutManager;
    private AppContactService appContactService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_recode);
        ButterKnife.bind(this);
//        EventBus.getDefault().register(this);
        init();
        initView();
        load();
    }


    @Override
    protected void onDestroy() {
//        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    private void load() {
        List<GetRoomUsersResponse.ListBean> list = ChatRecordUtils.getList();
        recodeChatAdapter.setList(list);
    }

    private void initView() {
        appContactService = new AppContactService(this);
        recodeChatAdapter = new RecodeChatAdapter(this);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        viewRecycler.setLayoutManager(linearLayoutManager);
        viewRecycler.setAdapter(recodeChatAdapter);
        viewRecycler.addOnItemTouchListener(new RecyclerItemClickListener(this) {
            @Override
            protected boolean onItemClick(View view, final int position) {
                final GetRoomUsersResponse.ListBean listBean = recodeChatAdapter.getList().get(position);
                if (!UserInfo.getUsername().equals(listBean.getUsername())) {
                    Builder builder = new Builder(ChatRecodeActivity.this);//首先自己不能和自己聊天，已经是管理员的人不能再被设为管理员，自己也不能移除自己
                    builder.addOption("私聊", Color.parseColor("#007aff"), new BottomDialog.OnOptionClickListener() {
                        @Override
                        public void onOptionClick() {
                            Intent intent = new Intent(ChatRecodeActivity.this, ConversationActivity.class);
                            if (!listBean.getChatroom_role().equals("1")) {
                                intent.putExtra("is_admin", true);
                            }
                            intent.putExtra("is_single", true);
                            intent.putExtra(ConversationUIService.USER_ID, listBean.getUsername());
                            startActivity(intent);
                            ChatRecordUtils.add(listBean);
                            SearchRecordUtils.add(listBean);
                        }
                    });
                    if (listBean.getChatroom_role() != null && listBean.getChatroom_role().equals("1")) {
                        builder.addOption("选定为管理员", Color.parseColor("#007aff"), new BottomDialog.OnOptionClickListener() {
                            @Override
                            public void onOptionClick() {
                                //sendAnalyticMsg("lottery_" + MobiComConversationFragment.ANALYTIC_VERSION + "_chat_group_set_admin");

                                Utils.createDialog(ChatRecodeActivity.this, "确定选定用户" + getDisplayName(listBean.getUsername()) + "为管理员",
                                        "取消", "确定",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {

                                            }
                                        }, new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                RetrofitBase.getRetrofit().create(AddChatAdminResquest.class)
                                                        .getResult(UserInfo.getToken(), listBean.getUsername(), 1)
                                                        .compose(RxHelper.<OperationSuccessResponse>handleResult())
                                                        .subscribe(new RxSubscribe<OperationSuccessResponse>(ChatRecodeActivity.this) {
                                                            @Override
                                                            protected void _onNext(OperationSuccessResponse operationSuccessResponse) {
                                                                Utils.showToast(operationSuccessResponse.getMsg());

                                                                listBean.setChatroom_role("3");
                                                                recodeChatAdapter.notifyDataSetChanged();
                                                            }

                                                            @Override
                                                            protected boolean showDialog() {
                                                                return true;
                                                            }
                                                        });
                                            }
                                        });
                            }
                        });
                    }
                    //如果是超级管理员，无法被移除
                    if (listBean.getChatroom_role() != null && !listBean.getChatroom_role().equals("3")) {
                        builder.addOption("移除此成员", Color.parseColor("#007aff"), new BottomDialog.OnOptionClickListener() {
                            @Override
                            public void onOptionClick() {
                                //sendAnalyticMsg("lottery_" + MobiComConversationFragment.ANALYTIC_VERSION + "_chat_group_remove_member");
                                Utils.createDialog(ChatRecodeActivity.this, "确定把用户" + getDisplayName(listBean.getUsername()) + "从群里移除",
                                        "取消", "确定",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {

                                            }
                                        }, new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                RetrofitBase.getRetrofit().create(ResumeChatUserRequest.class)
                                                        .getResult(UserInfo.getToken(), listBean.getUsername(), 0)
                                                        .compose(RxHelper.<OperationSuccessResponse>handleResult())
                                                        .subscribe(new RxSubscribe<OperationSuccessResponse>(ChatRecodeActivity.this) {
                                                            @Override
                                                            protected void _onNext(OperationSuccessResponse operationSuccessResponse) {
                                                                Utils.showToast(operationSuccessResponse.getMsg());

                                                                recodeChatAdapter.getList().remove(position);
                                                                recodeChatAdapter.notifyDataSetChanged();
                                                            }

                                                            @Override
                                                            protected boolean showDialog() {
                                                                return true;
                                                            }
                                                        });
                                            }
                                        });
                            }
                        });
                    }


                    if (listBean != null && listBean.getDataType() != 0) {
                        builder.addOption("删除该记录", Color.parseColor("#ff0000"), new BottomDialog.OnOptionClickListener() {
                            @Override
                            public void onOptionClick() {
                                //sendAnalyticMsg("lottery_" + MobiComConversationFragment.ANALYTIC_VERSION + "_chat_group_remove_member");
                                Utils.createDialog(ChatRecodeActivity.this, "确定把用户" + getDisplayName(listBean.getUsername()) + "的记录移除",
                                        "取消", "确定",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {

                                            }
                                        }, new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                if (listBean.getDataType() == 1) {
                                                    //删除聊天记录
                                                    ChatRecordUtils.delete(listBean.getUsername());
                                                    recodeChatAdapter.remove(listBean);
                                                } else if (listBean.getDataType() == 2) {
                                                    //删除搜索记录
                                                    SearchRecordUtils.delete(listBean.getUsername());
                                                }
                                            }
                                        });
                            }
                        });
                    }

                    builder.create().show();
                } else {
                    Utils.showToast("这是你本人噢");
                }
                return true;
            }
        });
    }

    private String getDisplayName(String username) {
        return appContactService.getContactById(username).getDisplayName();
    }
    private void init() {

    }

    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, ChatRecodeActivity.class);
        activity.startActivity(intent);

    }
}
